package com.github.xuemingyu.springboot.acticitycloud.controller.api;

import com.bidanet.bdcms.core.bean.ApiResult;
import com.bidanet.bdcms.core.vo.Page;
import com.github.xuemingyu.springboot.acticitycloud.entity.Registration;
import com.github.xuemingyu.springboot.acticitycloud.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: xmy
 * \* Date: 2019/1/24
 * \* Time: 13:37
 * \
 */
@RestController
@RequestMapping("/registration")
public class RegistrationApiController {
    @Autowired
    private RegistrationService registrationService;

    @RequestMapping("/queryLike")
    public ApiResult index(Registration registration, Page page) {
        try {
            registrationService.queryLike(registration, page);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success(page);
    }

    /**
     * 报名
     *
     * @param registration
     * @param request
     * @return
     */
    @RequestMapping("/joinin")
    public ApiResult add(Registration registration, HttpServletRequest request) {
        try {
            registration.setUserId(request.getHeader("userId"));
            registrationService.insert(registration);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("报名成功");
    }

    //取消报名
    @RequestMapping("/joinout")
    public ApiResult joinout(Long activityId, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        try {
            registrationService.joinout(activityId, userId);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("取消报名成功");
    }
    /**
     * 签到
     *
     * @param registration
     * @param request
     * @return
     */
    @RequestMapping("/sign")
    public ApiResult sign(Registration registration, HttpServletRequest request) {
        registration.setUserId(request.getHeader("userId"));
        try {
            registrationService.update(registration);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("签到成功");
    }

    @RequestMapping("/delete")
    public ApiResult delete(Long id) {
        try {
            registrationService.delete(id);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("删除成功");
    }
}
